import React, {Component} from 'react'
import {
    View,
    Text,
    Image,
    StyleSheet,
    ListView
} from 'react-native'
import CommonListItem from "./CommonListItem";
import CommonItem from "./CommonItem";


export default class SecondAdsView extends Component {
    constructor(props) {
        super(props)
        this.state = {
            itemList: []
        }

        this.fetchData()
    }

    fetchData() {
        fetch('http://localhost:8081/localdata/XMG_Home_D4.json')
            .then((response) => response.json())
            .then((responseJson) => {
                console.log(responseJson.data)
                this.setState({
                    itemList: responseJson.data
                })
            })
            .catch((e) => {
                console.error(e)
            })
    }

    render() {
        return (
            <View style={styles.container}>
                {this.renderCommonList()}
            </View>
        )
    }

    renderCommonList() {

        var items = []
        var datas = this.state.itemList
        console.log(' ---- 中间广告 ----')
        console.log(datas)
        for (var i = 0; i < datas.length; i++) {
            items.push(
                <CommonItem
                    key={i}
                    imgWidth={50}
                    imgHeight={50}
                    internetImage={this.dealWith(datas[i].imageurl)}
                    titleColor={datas[i].typeface_color}
                    title={datas[i].maintitle}
                    subTitle={datas[i].deputytitle}
                />
            )
        }
        return items;
    }

    dealWith(url){
        if(url.includes('w.h')){
            return url.replace('w.h', '35.35')
        }else{
            return url
        }
        // return url.inCluds('w.h')? url.replace('w.h', '30.80'): url;
    }
}

const styles = StyleSheet.create({

    container: {
        marginTop: 10,
        flexWrap:'wrap',
        height:140,
    }
})